package com.cw.data.handler;

import java.util.Vector;

import javax.swing.table.DefaultTableModel;

import com.cw.action.panel.CheckPanel;
import com.cw.action.panel.LtCheckPanel;
import com.cw.maintenance.AddDataDialog;
import com.cw.po.Lottery;

public class LtHandler extends AbstractHandler {

	public LtHandler(Lottery lot) {
		super(lot);
	}

	@Override
	public void loadData(Vector<Vector<String>> values, DefaultTableModel dtm) {
		int specNum = lottery.getSpecialnum();
		int minNum = Integer.parseInt(lottery.getMinnum());
		int maxNum = Integer.parseInt(lottery.getMaxnum());
		for(int i=0;i<values.size();i++){
			dtm.addRow(new Vector());
			Vector<String> vec = values.elementAt(i);
			for(int j=0;j<vec.size()-specNum;j++){
				String v = vec.get(j);
				if(j<2){
					dtm.setValueAt(v, i, j);
				}else{
					dtm.setValueAt(v, i, Integer.parseInt(v)+1);
				}
			}
			for(int j=0;j<specNum;j++){
				String v = vec.get(vec.size()-specNum+j);
				dtm.setValueAt(v, i, 3+maxNum-minNum+Integer.parseInt(v));
			}
		}
	}

	@Override
	public void insertData(Vector<String> vec, DefaultTableModel dtm) {
		dtm.addRow(new Vector());
		int row = dtm.getRowCount()-1;
		int specNum = lottery.getSpecialnum();
		int maxNum = Integer.parseInt(lottery.getMaxnum());
		int minNum = Integer.parseInt(lottery.getMinnum());
		for(int j=0;j<vec.size()-specNum;j++){
			String v = vec.get(j);
			if(j<2){
				v = v.substring(1, v.length()-1);
				dtm.setValueAt(v, row, j);
			}else{
				dtm.setValueAt(v, row, Integer.parseInt(v)+1);
			}
		}
		for(int j=0;j<specNum;j++){
			String v = vec.get(vec.size()-specNum+j);
			dtm.setValueAt(v, row, 3+maxNum-minNum+Integer.parseInt(v));
		}
	}

	@Override
	public CheckPanel getChkPanel(AddDataDialog add) {
		return new LtCheckPanel(add, lottery);
	}

	@Override
	public void createColumn(DefaultTableModel dtm, Lottery lt) {
		for(int i=Integer.parseInt(lt.getMinnum());i<=Integer.parseInt(lt.getMaxnum());i++){
			dtm.addColumn(convertInt2String(i));
		}
		if(lt.getMinspecial()!=null&&!lt.getMinspecial().equals("")){
			Integer min2 = Integer.parseInt(lt.getMinspecial());
			Integer max2 = Integer.parseInt(lt.getMaxspecial());
			if(max2>min2){
				dtm.addColumn("||");
				for(int i=min2;i<=max2;i++){
					dtm.addColumn(convertInt2String(i));
				}
			}
		}		
	}
	private String convertInt2String(int k){
		if(k<10){
			return "0"+k;
		}
		return String.valueOf(k);
	}
}
